// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()

const TcbRouter = require('tcb-router')

/**
 * 同一个云函数返回不同接口
 */
exports.main = async (event, context) => {
  const app = new TcbRouter({event})

  app.use(async(ctx, next) => {
    console.log('进入全局中间件')
    ctx.data = {}
    ctx.data.openId = event.userInfo.openId
    await next()
    console.log('退出全局中间件')
  })

  app.router('music', async(ctx, next) => {
    console.log('enter music name')
    ctx.data.musicName = '音乐名称'
    await next()
    console.log('quit music name')
  }, async (ctx, next) => {
    console.log('enter music type')
    ctx.data.musicType = '儿歌'
    ctx.body = {
      data: ctx.data
    }
    console.log('quit music type')
  })

  app.router('movie', async (ctx, next) => {
    ctx.data.movieName = '电影名称'
    await next()
  }, async (ctx, next) => {
    ctx.data.movieType = '喜剧'
    ctx.body = {
      data: ctx.data
    }
  })

  return app.serve()
}